﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using HotelManagement.Class;

namespace HotelManagement
{
    public partial class RetrieveHousekeepingMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                displayScheduleID();
            }
        }

        protected void btnRetrieveByMonth_Click(object sender, EventArgs e)
        {
            try
            {
                displayGridViewMonthly();
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Enter Date";
            }
           
        }

        protected void btnYearly_Click(object sender, EventArgs e)
        {
            try
            {
                displayGridViewYearly();
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Enter Date";
            }
            
        }

        protected void btnDaily_Click(object sender, EventArgs e)
        {
            try
            {
                displayGridViewDaily();
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Enter Date";
            }
            
        }

        protected void btnScheduleID_Click(object sender, EventArgs e)
        {
            try
            {
                Server.Transfer("RetrieveHousekeepingSchedule.aspx?scheduleID=" + lbScheduleID.SelectedValue);
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Select Schedule ID to Search";

            }
        }

        protected void btnGoBack_Click1(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        private void displayScheduleID()
        {
            lbScheduleID.Items.Clear();
            string selectSQL = "SELECT scheduleID , DATENAME(day, ScheduleDate) as [Day], DATENAME(month, ScheduleDate) as [Month], DATENAME(year, ScheduleDate) as [Year] FROM HousekeepingSchedule";

            SqlDataReader reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();

                SqlCommand cmd = new SqlCommand(selectSQL, con);
                ListItem newItem = new ListItem();


                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["scheduleID"].ToString() + " - " + reader["Day"].ToString() + " " + reader["Month"].ToString() + " " + reader["Year"].ToString();
                    newItem.Value = reader["scheduleID"].ToString();
                    lbScheduleID.Items.Add(newItem);
                }
                reader.Close();


            }
            catch (SqlException Ex1)
            {
                lblStatus.Text = "Error: " + Ex1 + "Please try again";
            }

        }

        private void displayGridViewMonthly()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();

            SqlConnection con = StaffDBManager.ConnectToDatabase();
            con.Open();

            da = new SqlDataAdapter("SELECT [scheduleID], GeneralMaintenanceStaffID, RoomMaintenanceStaffID, EstateMaintenanceStaffID, SecurityMaintenanceStaffID, Description, DATENAME(month, ScheduleDate) as [Month], DATENAME(year, ScheduleDate) as [Year] FROM [HousekeepingSchedule] WHERE DATENAME(month, ScheduleDate)='" + ddlMonthly.SelectedValue + "' AND DATENAME(year, ScheduleDate)='"+ tbxYearly0.Text +"'", con);

            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            con.Close();

        }

        private void displayGridViewYearly()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();

            SqlConnection con = StaffDBManager.ConnectToDatabase();
            con.Open();

            da = new SqlDataAdapter("SELECT [scheduleID], GeneralMaintenanceStaffID, RoomMaintenanceStaffID, EstateMaintenanceStaffID, SecurityMaintenanceStaffID, Description, DATENAME(year, ScheduleDate) as [Year] FROM [HousekeepingSchedule] WHERE DATENAME(year, ScheduleDate)='" + tbxYearly.Text + "'", con);

            da.Fill(ds);
        
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            con.Close();
            if (ds.Tables[0] == null)
            {
                lblStatus.Text = "No Data";
            }

        }

        private void displayGridViewDaily()
        {
            string date = tbxYear.Text + "-" + ddlMonth.SelectedValue + "-" + tbxDay.Text;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();

            SqlConnection con = StaffDBManager.ConnectToDatabase();
            con.Open();


            da = new SqlDataAdapter("SELECT [scheduleID], GeneralMaintenanceStaffID, RoomMaintenanceStaffID, EstateMaintenanceStaffID, SecurityMaintenanceStaffID, Description, DATENAME(day, ScheduleDate) as [Day], DATENAME(month, ScheduleDate) as [Month], DATENAME(year, ScheduleDate) as [Year] FROM HousekeepingSchedule WHERE ScheduleDate='" + date + "'", con);

            da.Fill(ds);

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            con.Close();
            if (ds.Tables[0] == null)
            {
                lblStatus.Text = "No Data";
            }

        }





    }
}